package com.qubuyer.business.home.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.blankj.utilcode.util.ConvertUtils
import com.qubuyer.R
import com.qubuyer.business.home.activity.SecondGoodListActivity
import com.qubuyer.business.home.adapter.HomeDailyDiscountAdapter
import com.qubuyer.business.home.adapter.HomeViewPagerAdapter
import com.qubuyer.databinding.HomeItemDailydiscountBinding
import com.qubuyer.repository.entitys.HomeGoodEntity
import com.qubuyer.utils.NavigationUtil
import com.qubuyer.utils.PageUtil
import java.util.*

class DailyDiscountItemView(lifecycleOwner: LifecycleOwner, context: Context, parent: ViewGroup, layoutId: Int) : HomeItemView<List<HomeGoodEntity>, HomeItemDailydiscountBinding>(lifecycleOwner, context, parent, layoutId) {
    override fun bindView(data: List<HomeGoodEntity>) {
        dataBinding.host = this
        val pageList: MutableList<View> = ArrayList()
        val pageUtil = PageUtil(data, 3)
        dataBinding.llIndicator.removeAllViews()
        if (pageUtil.getTotalPages() > 1) {
            dataBinding.llIndicator.visibility = View.VISIBLE
            var view: View
            for (i in 0 until pageUtil.getTotalPages()) {
                view = View(mContext)
                val layoutParams1 = LinearLayout.LayoutParams(ConvertUtils.dp2px(7f), ConvertUtils.dp2px(7f))
                layoutParams1.setMargins(ConvertUtils.dp2px(9f), 0, 0, 0)
                view.layoutParams = layoutParams1
                view.setBackgroundResource(R.drawable.home_super_return_indi_slt)
                if (i == 0) {
                    view.isSelected = true
                }
                dataBinding.llIndicator.addView(view)
            }
        } else {
            dataBinding.llIndicator.visibility = View.GONE
        }
        for (i in 0 until pageUtil.getTotalPages()) {
            val page = LayoutInflater.from(mContext).inflate(R.layout.layout_fragment_home_dailydiscount_page, null).apply {
                findViewById<RecyclerView>(R.id.rv_list).apply {
                    layoutManager = GridLayoutManager(mContext, 3)
                    adapter = HomeDailyDiscountAdapter(ArrayList(pageUtil.getObjects(i + 1)))
                }
            }
            pageList.add(page)
        }
        val mViewPagerAdapter = HomeViewPagerAdapter(pageList)
        dataBinding.vpDailydiscount.adapter = mViewPagerAdapter
        dataBinding.vpDailydiscount.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                val count = dataBinding.llIndicator.childCount
                for (i in 0 until count) {
                    dataBinding.llIndicator.getChildAt(i).isSelected = i == position
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        dataBinding.vpDailydiscount.currentItem = 0
    }

    fun viewMore(v: View) {
        NavigationUtil.overlay(mContext, SecondGoodListActivity::class.java, 6)
    }
}